package com.demo.admin.controller;

import com.demo.common.util.R;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author YT
 * @date 2019-07-01
 */
@RestController
@AllArgsConstructor
@RequestMapping("/mail")
public class MailSenderController {

    private final JavaMailSender mailSender;

    private final freemarker.template.Configuration freemarkerConfiguration;

    /**
     * 纯文本格式
     *
     * @return
     */
    @GetMapping("/simple")
    public R simpleSend() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("663837973@qq.com");
        message.setTo("663837973@qq.com");
        message.setSubject("主题：来自 YT 邮件");
        message.setText("这是测试 Spring Boot 集成 mail 的邮件");
        mailSender.send(message);
        return new R<>("发送成功");
    }

    /**
     * 附件格式
     *
     * @return
     */
    @GetMapping("/attach")
    @SneakyThrows
    public R attachSend() {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("663837973@qq.com");
        helper.setTo("663837973@qq.com");
        helper.setSubject("主题：来自 YT 邮件");
        helper.setText("(含附件) 这是测试 Spring Boot 集成 mail 的邮件");
        // 添加附件
        File file = new File("C:\\Users\\YT\\Desktop\\mail.txt");
        // 建议文件带上后缀，可以支持在线预览
        helper.addAttachment("mail.txt", file);
        mailSender.send(mimeMessage);
        return new R<>("发送成功");
    }

    /**
     * html 格式
     *
     * @return
     */
    @GetMapping("/html")
    @SneakyThrows
    public R htmlSend() {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("663837973@qq.com");
        helper.setTo("663837973@qq.com");
        helper.setSubject("主题：来自 YT 邮件");
        String html = "<html><body><div>这是测试 Spring Boot 集成 mail 的邮件</div><div><img src='cid:pic'></div></body></html>";
        helper.setText(html);
        // 添加附件
        File file = new File("C:\\Users\\YT\\Desktop\\1.jpg");
        helper.addAttachment("1.jpg", file);
        helper.addInline("pic", file);
        mailSender.send(mimeMessage);
        return new R<>("发送成功");
    }

    /**
     * 使用 freemarker 邮件模板格式
     *
     * @param userName
     * @return
     */
    @GetMapping("/template")
    @SneakyThrows
    public R template(String userName) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("663837973@qq.com");
        helper.setTo("498238867@qq.com");
        helper.setSubject("主题：来自 YT 邮件");
        //设置替换的参数对象
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("userName", StringUtils.isEmpty(userName) ? "test" : userName);
        String templateString = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate("mail.ftl"), model);
        helper.setText(templateString, true);
        // 添加附件
        File file = new File("C:\\Users\\YT\\Desktop\\mail.txt");
        // 建议文件带上后缀，可以支持在线预览
        helper.addAttachment("mail.txt", file);
        helper.addInline("pic", file);
        mailSender.send(mimeMessage);
        return new R<>("发送成功");
    }

}
